"""
 -*- coding: utf-8-*-
 @File: page_project_mgt.py
 @Author: chen
 @time: 2023/12/03/ 10:51
"""
import time

from stage_L1.lim_project.page_po.businesses.page_base import BasePage
from stage_L1.lim_project.page_po.businesses.page_login import LoginPage
from stage_L1.lim_project.page_po.utils.mgt_logger import logger


class ProjectMgtPage(BasePage):
    def goto_create(self):
        _btn_create = (self.BY.XPATH, '//*[text()="新建"]')
        logger.info("点击创建按钮")
        self.do_click(_btn_create)
        from stage_L1.lim_project.page_po.businesses.page_project.page_create import CreatePage
        logger.info("成功进入创建项目页面")
        return CreatePage(self.driver)

    def goto_update(self, project_name):
        _btn_update = (self.BY.XPATH, rf'//*[text()="{project_name}"]/..//*[text()="修改"]')
        logger.info(f"点击项目【{project_name}】的修改按钮")
        self.do_click(_btn_update)
        from stage_L1.lim_project.page_po.businesses.page_project.page_create import CreatePage
        logger.info("成功进入修改项目页面")
        return CreatePage(self.driver)

    def _delete_project01(self, project_name):
        _btn_delete = (self.BY.XPATH, rf'//*[text()="{project_name}"]/..//*[text()=" 删除 "]')
        _btn_ensure = (self.BY.XPATH, '//*[text()="确 定"]')
        logger.info(f"点击项目【{project_name}】的删除按钮")
        self.do_click(_btn_delete)
        logger.info("点击确定按钮")
        self.do_click(_btn_ensure)
        return self

    def _delete_project02(self, project_name):
        _btn_delete = (self.BY.XPATH, rf"//*[text()='{project_name}']/..//*[text()=' 删除 ']")
        _btn_ensure = (self.BY.XPATH, '//*[text()="确 定"]')
        logger.info(f"点击项目【{project_name}】的删除按钮")
        self.do_click(_btn_delete)
        logger.info("点击确定按钮")
        self.do_click(_btn_ensure)
        return self

    def project_exist(self, project_name):
        """判断项目是否存在"""
        if project_name == '\"':
            _span_project = (self.BY.XPATH, rf"//*[text()='{project_name}']/..//*[text()=' 删除 ']")
        else:
            _span_project = (self.BY.XPATH, rf'//*[text()="{project_name}"]/..//*[text()=" 删除 "]')
        logger.info(f"查找元素")
        ele = self.do_finds(_span_project)
        if ele:
            logger.info(f"元素【{_span_project}】--->存在")
            return True
        else:
            logger.info(f"元素【{_span_project}】--->不存在")
            return False

    def create(self, project_name):
        self.goto_create(). \
            input_name(project_name). \
            click_ensure()
        logger.info(f"创建项目【{project_name}】--->成功")

    def update(self, old_name, new_name):
        if self.project_exist(old_name):
            self.goto_update(old_name). \
                input_name(new_name). \
                click_ensure()
        else:
            logger.info(f"项目【{old_name}】--->不存在")

    def delete(self, project_name):
        if project_name == '\"':
            self._delete_project02(project_name)
            logger.info(f"删除项目【{project_name}】--->成功")
        elif self.project_exist(project_name):
            self._delete_project01(project_name)
            logger.info(f"删除项目【{project_name}】--->成功")
        time.sleep(0.5)


if __name__ == '__main__':
    cur_page = LoginPage().login().goto_project()
    name1 = "cc_01"
    name2 = "cc_02"
    cur_page.create(name1)
    cur_page.update(name1, name2)
    cur_page.delete(name2)
    cur_page.delete("\"")
    cur_page.delete("'")
    cur_page.do_quit()
